from src.data import DataSeq
import time
import random

random.seed(42)

MAX_TIME = 30


def MonkeySort(ds,v):
    Length = ds.length
    while True:
        i = 1
        while i < Length:
            if ds.data[i - 1] < ds.data[i]:
                i += 1
            else:
                break
        if i == Length or ds.time > MAX_TIME:
            break
        random.shuffle(ds.data)
        ds.Getfigure()
        idx = random.randint(0, ds.length - 1)
        ds.SetVal(idx, ds.data[idx])
        time.sleep(v)


def create_sort(title, v, l):
    ds = DataSeq(l, sort_title=title)
    ds.Visualize()
    ds.StartTimer()
    MonkeySort(ds, v)
    ds.StopTimer()
    ds.SetTimeInterval(0)
    # ds.Visualize()
    time.sleep(3)
    ds.destory()
    return 0

if __name__ == "__main__":
    ds = DataSeq(64, repeatition=True)
    ds.Visualize()
    ds.StartTimer()
    MonkeySort(ds)
    ds.StopTimer()
    ds.SetTimeInterval(0)
    ds.Visualize()
